#include <string.h>
#include <iostream>

using std::cout;
using std::endl;

void test()
{
    char arr[] = {'h', 'e','l', 'l','o', '\0'};
    char arr2[] = "world";
    cout << "strlen(arr) = " << strlen(arr) << endl;
    cout << "strlen(arr2) = " << strlen(arr2) << endl;

    //想将arr与arr2的内容拼接起来，改怎么做呢
    size_t len1 = strlen(arr);
    size_t len2 = strlen(arr2);
    size_t len = len1 + len2 + 1;//申请长度
    //申请空间
    char * pstr = static_cast<char *>(malloc(len));
    //初始化
    memset(pstr, 0, len);
    strcpy(pstr, arr);//拷贝
    strcat(pstr, arr2);//拼接
    printf("pstr = %s\n", pstr);

    free(pstr);
    pstr = NULL;//悬空指针的问题
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

